using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Configuration.Provider;

/// <summary>
/// Summary description for SqlPersonalizationProvider
/// </summary>
public class SqlPersonalizationProvider : PersonalizationProvider
{
    public SqlPersonalizationProvider() { }

    public override string ApplicationName
    {
        get { throw new NotSupportedException(); }
        set { throw new NotSupportedException(); }
    }

    public override void Initialize(string name, NameValueCollection config)
    {
        // Verify that config isn't null
        if (config == null) throw new ArgumentNullException("config");

        // Assign the provider a default name if it doesn't have one
        if (String.IsNullOrEmpty(name)) name = "SqlPersonalizationProvider";

        // Add a default "description" attribute to config if the
        // attribute doesn't exist or is empty
        if (string.IsNullOrEmpty(config["description"]))
        {
            config.Remove("description");
            config.Add("description", "SQL Server Personalization Provider");
        }

        // Call the base class's Initialize method
        base.Initialize(name, config);

        // Throw an exception if unrecognized attributes remain
        if (config.Count > 0)
        {
            string attr = config.GetKey(0);
            if (!String.IsNullOrEmpty(attr)) throw new ProviderException("Unrecognized attribute: " + attr);
        }
    }

    protected override void LoadPersonalizationBlobs(
        WebPartManager webPartManager, string path, string userName,
        ref byte[] sharedDataBlob, ref byte[] userDataBlob)
    {
        userName = UserUtils.GetLoggedID();
        if (!String.IsNullOrEmpty(userName))
        {
            Personalization dto = new Personalization(userName, path, null);
            new PersonalizationBus().LoadPersonalizationBlobs(dto);
            userDataBlob = dto.PageSettings;
        }
    }

    protected override void ResetPersonalizationBlob
        (WebPartManager webPartManager, string path, string userName)
    {
        userName = UserUtils.GetLoggedID();
        Personalization dto = new Personalization(userName, path, null);
        new PersonalizationBus().ResetPersonalizationBlob(dto);
    }

    protected override void SavePersonalizationBlob
        (WebPartManager webPartManager, string path, string userName, byte[] dataBlob)
    {
        userName = UserUtils.GetLoggedID();
        Personalization dto = new Personalization(userName, path, dataBlob);
        new PersonalizationBus().SavePersonalizationBlob(dto);
    }

    public override PersonalizationStateInfoCollection FindState
        (PersonalizationScope scope, PersonalizationStateQuery query,
        int pageIndex, int pageSize, out int totalRecords)
    {
        throw new NotSupportedException();
    }

    public override int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query)
    {
        throw new NotSupportedException();
    }

    public override int ResetState(PersonalizationScope scope, string[] paths, string[] usernames)
    {
        throw new NotSupportedException();
    }

    public override int ResetUserState(string path, DateTime userInactiveSinceDate)
    {
        throw new NotSupportedException();
    }
}
